#!/usr/bin/env python27
# -*- coding: utf-8 -*-
# app_zp_rock_owner_cost_4mail_v2
# @author xuhualei
# @date 2023.07.19

import os, sys
from datetime import datetime, timedelta
#and owner_id in ('xuhualei','lixiyong01','lishujie','wanglizhou','guopeng09','lvruichao','yangxing04','jihailong','yulei12','shaodongyang','zhengbo05','tongdanyu','wuguangxi','liyongqiang06','liuxiaojie05')
############ 表存储计算成本 ############
def read_data(dt):
    sql="""
    select
    dt,owner_id,total_storage_score,total_storage_tbls,rock_storage_tbls,
    round(total_storage_filesize,2) as total_storage_filesize,
    round(total_storage_money,2) as total_storage_money,
    total_task_score,total_tasks,rock_tasks,
    round(total_tasks_filesize,2) as total_tasks_filesize,
    round(total_tasks_money,2) as total_tasks_money
    from hdp_lbg_supin_zplisting.app_zp_rock_owner_health_filesize_task_money
    where dt='%s' and owner_id='lixiyong01';
    """ % (dt)
    os.system('hive -e "'+str(sql)+'">./result1.txt')
    results = []
    f=open("./result1.txt","r")
    for line in f:
        rows=line.split('\t')
        results.append(rows)
    return results

if __name__=="__main__":
    yesterday=datetime.now() - timedelta(1)
    dt=datetime.strftime(yesterday, '%Y%m%d')
    results = read_data(dt)
    for rows in results:
        header='日期:%s, 姓名:%s'% (rows[0],rows[1])
        title1='<td>存储健康分</td><td>总表数</td><td>总存储(T)</td><td>存储成本(元/TB/天)</td><td>未设置生命周期表</td>'
        content1='<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>' % (rows[2],rows[3],rows[5],rows[6],rows[4])
        title2='<td>任务健康分</td><td>总任务数</td><td>CPU(核*小时)</td><td>计算成本(元/CU/月)</td><td>超3小时任务</td>'
        content2='<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>' % (rows[7],rows[8],rows[10],rows[11],rows[9])
        username=str(rows[1])
        receiver1='%s@58.com' % (username)

        content = """
            <h2>个人存储任务账单如下</h2>
            <h3>%s</h3>
            <table border='1' cellspacing='0' cellpadding='7px'>
            <tr style='font-weight: bold' bgcolor='#dcdcdc'>%s</tr>
            <tr>%s</tr>
            <tr style='font-weight: bold' bgcolor='#dcdcdc'>%s</tr>
            <tr>%s</tr>
            </table>
            <h2>相关链接:</h2>
            <a href='https://star-zp.58corp.com/sre/index.html#/star/cost/health_score_list2/store/%s/0/%s'>个人存储健康分</a>
            <a href='https://star-zp.58corp.com/sre/index.html#/star/cost/health_score_list2/task/%s/0/%s'>个人任务健康分</a>

            <p>负责人：lixiyong01, xuhualei</p>
            """ %(header, title1, content1, title2, content2, dt, username, dt, username)
        print content
        cmdFmt="""curl -X POST -d \"oaname=%s&subject=个人存储任务账单&content=%s&token=d28dd236b6f6e89e4145882e4d227d9e\" http://openmsg.monitor.op.58dns.org/channel/email
        """ %(receiver1,content)
        print cmdFmt
        os.system(cmdFmt)
